1.2 - C# Variables


What is a variable?

A variable is basically a container which stores a value. This value can be of different types, going from numbers, text and many others.
To declare (create) a variable all we need to do is typing:

  • It's type
  • It's name
  • It's initial value (not always required)

Variable types

Here is a list of different variable types you will surely use when making a mod:


int x = 5;
float y = 2.5f;
bool b = true;
string str = "Hello";
            

As you can see we have four different variable types highlighted in orange, each one followed by it's name (x, y, b, str)
and it's value.

The int variable type

int stands for integer, this means that this variable will store an integer number like 2, 10, 999999 etc.

The float variable type

float stands for decimal number, this means that this variable will store a decimal number like 2,5 or 7,2 etc. The actual value always needs an f after it.

The bool variable type

bool stands for boolean, it's a variable which can store either 1 (true) or 0 (false). This is used a lot in cases when you need to toggle something on or off.
The available value is either true or false.

The string variable type

string basically stands for text, it's a variable which stores text like the one you are reading. The actual text always need to be put inside double quotes "

Variables default values

If we don't give a value to a declared variable, it will default to a predefined value.

  • int will default to 0
  • float will default to 0
  • bool will default to false
  • string must need a default value which can also be an empty string like "" or using string.empty